#! /usr/bin/env python

# Copyright (c) 2011 - 2012 Tony Bai <bigwhite.cn@gmail.com>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os

from utils import env
from utils import config
from utils import command
from utils import errnos
from utils import options

from core import core
from core import pack
from core.core import BIT64

def buildc_run():
    buildc_home = os.path.dirname(os.path.realpath(__file__))
    opts, args = options.options_parse()

    if args[0] == 'init':
        core.init(buildc_home)
        sys.exit(0)

    if args[0] == 'cache':
        if args[1] == 'init':
            core.cache_init(opts.cmode, opts.ignore_error)
            sys.exit(0)

        if args[1] == 'update':
            core.cache_update(opts.cmode, opts.ignore_error)
            sys.exit(0)

        if args[1] == 'upgrade':
            core.cache_upgrade(opts.cmode, opts.ignore_error)
            sys.exit(0)

        if args[1] == 'remove':
            core.cache_remove(opts.cmode)
            sys.exit(0)

        print "Error: command [cache] does not support arg [%s]" % args[1]
        sys.exit(errnos.errors['args_invalid'])

    if args[0] == 'config':
        if args[1] == 'init':
            core.config_init(buildc_home)
            sys.exit(0)

        if args[1] == 'make':
            if opts.cmode == None:
                opts.cmode = BIT64
            core.config_make(buildc_home, opts.cmode)
            sys.exit(0)

        print "Error: command [config] does not support arg [%s]" % args[1]
        sys.exit(errnos.errors['args_invalid'])

    if args[0] == 'pack':
        if args[1] == 'create':
            pack.pack_create(buildc_home, opts)
            sys.exit(0)

        if args[1] == 'build':
            if opts.cmode == None:
                opts.cmode = BIT64
            pack.pack_build(buildc_home, opts)
            sys.exit(0)

        if args[1] == 'clean':
            pack.pack_clean()
            sys.exit(0)

        if args[1] == 'upload':
            pack.pack_upload(opts)
            sys.exit(0)

        if args[1] == 'source':
            if opts.cmode == None:
                opts.cmode = BIT64
            pack.pack_source(buildc_home, opts)
            sys.exit(0)

        print "Error: command [pack] does not support arg [%s]" % args[1]
        sys.exit(errnos.errors['args_invalid'])

if __name__ == '__main__':
    buildc_run()
